import { Observable, Subject } from "rxjs";
import { createSubscriber, createPublisher } from "@core/redis/message.wrapper";
import { RedisSubscriber } from "@core/redis/redis.subscriber";
import { RedisPublisher } from "@core/redis/redis.publisher";
import { eventChannelName } from "@events/event.channel";
import { OrderModel } from "@models/order.model";
import { OrderRepository } from "@repositories/order.repository";
import { ServiceBase } from "@services/servise.base";

class OrderPersistService extends ServiceBase {
  private static instance: OrderPersistService;
  private repository = new OrderRepository();
  private subscriber: RedisSubscriber | undefined;
  private publisher: RedisPublisher | undefined;

  private constructor() {
    super();
  }

  public static getInstance(): OrderPersistService {
    if (!OrderPersistService.instance) {
      OrderPersistService.instance = new OrderPersistService();
    }

    return OrderPersistService.instance;
  }

  protected init(): void {
    this.publisher = createPublisher();
    this.subscriber = createSubscriber();

    const observable = this.subscriber.subscribe<OrderModel>(
      eventChannelName.orderEventChannel.order_command
    );
    
    observable.subscribe({
      next: (order: OrderModel) => {
        
      },
    });
  }

  protected concreteDispose(): void {
    this.publisher?.dispose();
    this.subscriber?.dispose();
  }
}
